//
//  ClubOtherInfoViewController.swift
//  77Club
//
//  Created by Apple on 2024/10/22.
//

import UIKit
import ZLPhotoBrowser

class ClubOtherInfoViewController: ClubBaseViewController, RaeAnimationProtocol {

    private let backButton = c_make_ex(UIButton.self)
        .image("Common_whiteBack").end()
    private let moreButton = c_make_ex(UIButton.self)
        .image("Mine_other_more").end()
    private let topbackImage = c_make_ex(UIImageView.self)
        .image("Mine_info_bg").mode(.scaleAspectFill).end()
    private let iconCircleView = c_make_ex(UIView.self)
        .bgColor(.clear).corner(28.0.Scale)
        .border(.white.alpha(0.2), width: 2.0.Scale).end()
    private let shadowView = c_make_ex(UIView.self)
        .bgColor(.black.alpha(0.1)).end()
    private let iconImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(24.0.Scale).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color(.white).font(16.font_semiBold).alignment(.center).end()
    private let signLabel = c_make_ex(UILabel.self)
        .color(.white).font(11.font_medium).alignment(.center).end()
    private let subLabel = UIIconButton(style: .leading(itemSpace: 0))
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = "#EDEEF0".toColor
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubDynamicListCell.self,ClubInfoEmptyCell.self,ClubSquareForumCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 306.0.Scale
//        tableView.contentInset = UIEdgeInsets(top: .navi_height + 210, left: 0, bottom: 0, right: 0)
        tableView.bounces = false
        return tableView
    }()
    
    private var dataSource = [ClubGameCircleModel]()
    private var forumData = [ClubGameCommunityListModel]()
    private var pageIndex = 1
    private let topH = .navi_height + 210
    private var type = "1"
    
    private var playingIndexPath: IndexPath?
    private var currentVC = true
    
    var isDetailFrom = false
    
    private var userModel: ClubMineUserModel?
    var uid: String = ""{
        didSet{
            loadData()
        }
    }
    
    var updateBlock: (() -> Void)?
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        topbackImage.isHidden = false
        currentVC = true
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        topbackImage.isHidden = true
        currentVC = false
        NotificationCenter.default.post(name: .stopForumAudioPlayer, object: nil)
        NotificationCenter.default.post(name: .stopDynamicAudioPlayer, object: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.rae.corner(12.0.Scale, corners: [.layerMinXMinYCorner,.layerMaxXMinYCorner])
        
        view.subviews{
            topbackImage
            backButton
            moreButton
            iconCircleView.subviews{
                iconImageView.subviews{
                    shadowView
                }
            }
            nameLabel
            signLabel
            subLabel
            tableView
        }
        
        backButton.leading(0).top(.statusHeight).width(56).height(44)
        moreButton.trailing(0).top(.statusHeight).width(56).height(44)
        topbackImage.leading(0).trailing(0).top(0).height(.screenWidth - 60)
        iconCircleView.top(.navi_height + 70.0.Scale).size(56.0.Scale).centerHorizontally()
        iconImageView.size(48.0.Scale).centerVertically().centerHorizontally()
        shadowView.fillContainer()
        nameLabel.width(.screenWidth - 70).centerHorizontally()
        nameLabel.Top == iconCircleView.Bottom + 4
        signLabel.width(.screenWidth - 70).centerHorizontally()
        signLabel.Top == nameLabel.Bottom + 4
        subLabel.width(.screenWidth - 70).centerHorizontally().height(12)
        subLabel.Top == signLabel.Bottom + 4
        tableView.leading(0).trailing(0).bottom(0).top(.navi_height + 210)
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: .safe_bottom, right: 0)
        
        subLabel.style { v in
            v.setTitleColor(.white, for: .normal)
            v.iconSize = CGSize(width: 12, height: 12)
            v.titleLabel?.font = 10.font_medium
            v.isUserInteractionEnabled = false
        }
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        moreButton.addTarget(self, action: #selector(clickMoreButton), for: .touchUpInside)
        
        let topView = ClubUserInfoTopView(frame: CGRectMake(0, 0, .screenWidth, 55))
        topView.updateCateData = { ty in
            self.type = ty
            self.loadNewData()
        }
        tableView.tableHeaderView = topView
        tableView.refresh_loadMore {
            self.loadMoreData()
        }.refresh_pull {
            self.loadNewData()
        }
    }
}

//MARK: - UITableViewDelegate, UITableViewDataSource
extension ClubOtherInfoViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if type == "1" {
            if self.dataSource.count == 0 {
                return 1
            }else{
                return self.dataSource.count
            }
        }else{
            if self.forumData.count == 0 {
                return 1
            }else{
                return self.forumData.count
            }
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if (self.dataSource.count == 0 && self.type == "1") || (self.forumData.count == 0 && self.type == "2") {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ClubInfoEmptyCell", for: indexPath) as! ClubInfoEmptyCell
            return cell
        }else{
            if type == "1" {
                //动态
                let cell = tableView.dequeueReusableCell(withIdentifier: "ClubDynamicListCell", for: indexPath) as! ClubDynamicListCell
                cell.circleModel = self.dataSource[indexPath.row]
                cell.jumpToDetail = { id in
                    if let uid = UserDefaultsManager.getString(forKey: .uid), uid.count > 0 {
                        let detailController = ClubDynamicDetailViewController()
                        detailController.dynamicID = id
                        self.navigationController?.pushViewController(detailController, animated: true)
                    }else{
                        self.navigationController?.pushViewController(ClubLoginViewController(), animated: true)
                    }
                }
                cell.toTopicDetailVC = { topicId in
                    let detailVC = ClubTopicDetailViewController()
                    detailVC.topicId = topicId
                    self.navigationController?.pushViewController(detailVC, animated: true)
                }
                cell.didSelectImage = { index,images in
                    self.selectImage(index: index, images: images)
                }
                cell.didSelectVideo = { url in
                    self.selectVideo(url: url)
                }
                cell.toGameDetailVC = { gid in
                    if let uid = UserDefaultsManager.getString(forKey: .uid), uid.count > 0 {
                        let detailVC = ClubGameDetailViewController()
                        detailVC.gameId = gid
                        self.navigationController?.pushViewController(detailVC, animated: true)
                    }else{
                        self.navigationController?.pushViewController(ClubLoginViewController(), animated: true)
                    }
                }
                cell.showMoreView = { md in
                    let alert = UIAlertController(title: "提示", message: "确定举报此动态吗？", preferredStyle: .alert)
                    // 添加一个按钮
                    alert.addAction(UIAlertAction(title: "确认", style: .default, handler: { _ in
                        if let uid = UserDefaultsManager.getString(forKey: .uid), uid.count > 0 {
                            let reportVC = ClubReportViewController()
                            reportVC.dynamicID = md.id
//                            reportVC.updateDataBlock = {
//                                self.loadNewData()
//                            }
                            self.present(reportVC, animated: true)
                        }else{
                            self.navigationController?.pushViewController(ClubLoginViewController(), animated: true)
                        }
                    }))
                    // 添加取消按钮
                    alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
                    // 显示弹窗
                    self.present(alert, animated: true, completion: nil)

                }
                return cell
            }else{
                
                let cell = tableView.dequeueReusableCell(withIdentifier: "ClubSquareForumCell", for: indexPath) as! ClubSquareForumCell
                cell.model = forumData[indexPath.row]
                cell.zanButtonClick = { postId in
                    self.likePost(ID: postId)
                }
                cell.jumpToDetail = {postId, uid in
                    self.toDetailVC(newId: postId, uid: uid)
                }
                cell.didSelectImage = { index,images in
                    self.selectImage(index: index, images: images)
                }
                cell.didSelectVideo = { url in
                    self.selectVideo(url: url)
                }
                cell.toGameDetailVC = { gid in
                    let detailVC = ClubGameDetailViewController()
                    detailVC.gameId = gid
                    self.navigationController?.pushViewController(detailVC, animated: true)
                }
                cell.toTopicDetailVC = { topicId in
                    let detailVC = ClubTopicDetailViewController()
                    detailVC.topicId = topicId
                    self.navigationController?.pushViewController(detailVC, animated: true)
                }
                cell.showMoreView = { md in
                    self.showMoreView(md: md)
                }
                return cell
            }
            
        }
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if self.dataSource.count > 0 && type == "1" {
            let detailController = ClubDynamicDetailViewController()
            detailController.dynamicID = dataSource[indexPath.row].id
            detailController.updateDataBlock = {
                self.loadNewData()
            }
            self.navigationController?.pushViewController(detailController, animated: true)
        }else if self.forumData.count > 0 && type == "2" {
            let detailVC = ClubSquareForumDetailViewController()
            detailVC.postID = forumData[indexPath.row].id
            detailVC.likeBlock = {
                self.loadNewData()
            }
            detailVC.updateDataBlock = {
                self.loadNewData()
            }
            self.navigationController?.pushViewController(detailVC, animated: true)
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let offsetY = scrollView.contentOffset.y
        if offsetY >= 0 {
            if topH - offsetY <= .navi_height {
                tableView.topConstraint?.constant = .navi_height
            }else{
                tableView.topConstraint?.constant = topH - offsetY
            }
        }else{
            tableView.topConstraint?.constant = topH
        }
        
        
        // 获取 TableView 的中心位置
        let tableViewCenter = tableView.bounds.size.height / 2 + tableView.contentOffset.y
        
        if type == "1" {
            //动态
            guard let visibleCells = tableView.visibleCells as? [ClubDynamicListCell] else { return }
            for cell in visibleCells {
                guard let indexPath = tableView.indexPath(for: cell) else { continue }
                let cellCenterY = cell.center.y

                // 判断 Cell 中心是否接近 TableView 中心
                if tableView.contentOffset.y == 0 {
                    loadFirstVideo()
                }
                if abs(cellCenterY - tableViewCenter) < 100 { // 设定 50 为接近程度的阈值
                    if playingIndexPath != indexPath {
                        playingIndexPath = indexPath
                        cell.playVideo()  // 播放视频
                    }
                } else {
                    if playingIndexPath == indexPath {
                        cell.pauseVideo()  // 暂停视频
                        playingIndexPath = nil
                    }
                }
            }
        }else{
            //帖子
            guard let visibleCells = tableView.visibleCells as? [ClubSquareForumCell] else { return }
            for cell in visibleCells {
                guard let indexPath = tableView.indexPath(for: cell) else { continue }
                let cellCenterY = cell.center.y

                // 判断 Cell 中心是否接近 TableView 中心
                if tableView.contentOffset.y == 0 {
                    loadFirstVideo()
                }
                if abs(cellCenterY - tableViewCenter) < 100 { // 设定 50 为接近程度的阈值
                    if playingIndexPath != indexPath {
                        playingIndexPath = indexPath
                        cell.playVideo()  // 播放视频
                    }
                } else {
                    if playingIndexPath == indexPath {
                        cell.pauseVideo()  // 暂停视频
                        playingIndexPath = nil
                    }
                }
            }
        }
        
    }
}


//MARK: action
extension ClubOtherInfoViewController {
    
    private func selectImage(index: Int, images: [String]) {
        
        var datas: [Any] = []
        for a in images {
            datas.append(URL(string: a)!)
        }
        let vc = ZLImagePreviewController(datas: datas, index: index, showSelectBtn: false,showBottomView: false) { (url) -> ZLURLType in
            
            return .image
        } urlImageLoader: { (url, imageView, progress, loadFinish) in
            imageView.kf.setImage(with: url) { (receivedSize, totalSize) in
                let percentage = (CGFloat(receivedSize) / CGFloat(totalSize))
                debugPrint("\(percentage)")
                progress(percentage)
            } completionHandler: { (_) in
                loadFinish()
            }
        }
        
        vc.doneBlock = { (datas) in
            debugPrint(datas)
        }
        
        vc.modalPresentationStyle = .fullScreen
        self.showDetailViewController(vc, sender: nil)
    }
    
    private func selectVideo(url: String) {
        
        let vc = ZLImagePreviewController(datas: [URL(string: url)!], index: 0, showSelectBtn: false,showBottomView: false) { (url) -> ZLURLType in
            
            return .video
        } urlImageLoader: { (url, imageView, progress, loadFinish) in
            imageView.kf.setImage(with: url) { (receivedSize, totalSize) in
                let percentage = (CGFloat(receivedSize) / CGFloat(totalSize))
                debugPrint("\(percentage)")
                progress(percentage)
            } completionHandler: { (_) in
                loadFinish()
            }
        }
        
        vc.doneBlock = { (datas) in
            debugPrint(datas)
        }
        
        vc.modalPresentationStyle = .fullScreen
        self.showDetailViewController(vc, sender: nil)
    }
    
    @objc private func clickMoreButton(){
         let moreView = ClubOtherInfoMoreView()
         moreView.model = self.userModel
         moreView.blockBlock = {
             if let uid = UserDefaultsManager.getString(forKey: .uid), uid.count > 0 {
                 self.showSureView()
             }else{
                 self.navigationController?.pushViewController(ClubLoginViewController(), animated: true)
             }             
         }
         self.entryAnimate.animation(show: moreView)
     }
     
     private func showSureView(){
         let sureView = ClubOtherInfoBlockView()
         sureView.model = self.userModel
         sureView.sureBlock = {
             self.blackUser()
         }
         self.entryAnimate.animation(show: sureView)
     }
    
    private func shareDetail(type: Int, shareUrl: String) {
        if type == 3 {
            ClubGlobalTool.shared.shareToQQ(title: ClubGlobalTool.shared.dynamicShareTitle, description: ClubGlobalTool.shared.dynamicShareDes, imageUrl: "https://www.77.top/static/ios_logo.jpg", url: shareUrl)
        }else if type == 4 {
            "分享口令已复制成功".toast_success
            UIPasteboard.general.string = shareUrl
        }else{
            ClubGlobalTool.shared.shareToWechat(title: ClubGlobalTool.shared.dynamicShareTitle, des: ClubGlobalTool.shared.dynamicShareDes, image: "AppIcon".toImage!, shareUrl: shareUrl, shareType: type)
        }
    }
    
    private func likePost(ID: String){
        if let uid = UserDefaultsManager.getString(forKey: .uid), uid.count > 0 {
            Task{
                ToastUtil.show()
                let _ = await self.network.request(.likeStrategyPost(pid: ID, puid: "", type: "0"))
                ToastUtil.dismiss()
                self.loadNewData()
            }
        }else{
            self.navigationController?.pushViewController(ClubLoginViewController(), animated: true)
        }
        
    }
    
    private func toDetailVC(newId: String, uid: String){
        
        if uid != "0" {
            //用户
            let detailVC = ClubSquareForumDetailViewController()
            detailVC.postID = newId
            detailVC.likeBlock = {
                self.loadNewData()
            }
            detailVC.updateDataBlock = {
                self.loadNewData()
            }
            self.navigationController?.pushViewController(detailVC, animated: true)
        }else{
            //官方
            let detailVC = ClubForumDetailViewController()
            detailVC.newId = newId
            detailVC.likeBlock = {
                self.loadNewData()
            }
            self.navigationController?.pushViewController(detailVC, animated: true)
        }
    }
    
    private func showMoreView(md: ClubGameCommunityListModel){
        
        if md.status != 1 {
            let delView = ClubForumShareDelView()
            delView.delBlock = {
                Task{
                    let res = await self.network.request(.delPost(postID: md.id))
                    if res.isSuccess {
                        "删除成功".toast_success
                        self.loadNewData()
                        self.navigationController?.popViewController(animated: true)
                    }
                }
            }
            self.entryAnimate.animation(show: delView)
        }else{
            let moreView = ClubMoreView()
            moreView.isPost = true
            moreView.uid_des = md.uid_des
            moreView.reportBlock = {
                if let uid = UserDefaultsManager.getString(forKey: .uid), uid.count > 0 {
                    let reportVC = ClubReportViewController()
                    reportVC.dynamicID = md.id
                    reportVC.type = "0"
                    reportVC.isShequ = true
                    reportVC.updateDataBlock = {
                        self.navigationController?.popViewController(animated: true)
                    }
                    self.present(reportVC, animated: true)
                }else{
                    //登录
                    self.navigationController?.pushViewController(ClubLoginViewController(), animated: true)
                }
            }
            moreView.delBlock = {
                Task{
                    let res = await self.network.request(.delPost(postID: md.id))
                    if res.isSuccess {
                        "删除成功".toast_success
                        self.loadNewData()
                        self.navigationController?.popViewController(animated: true)
                    }
                }
            }
            moreView.copyShareBlock = { type in
                self.shareDetail(type: type, model: md)
            }
            moreView.moreBlock = { type in
                if type == 3 {
                    //下架
                    Task {
                        let res = await self.network.request(.postUpOrDown(id: md.id))
                        if res.isSuccess {
                            self.loadNewData()
                            self.navigationController?.popViewController(animated: true)
                        }
                    }
                }
            }
            self.entryAnimate.animation(show: moreView)
        }
        
    }
    
    private func shareDetail(type: Int, model: ClubGameCommunityListModel) {
        if type == 3 {
            ClubGlobalTool.shared.shareToQQ(title: ClubGlobalTool.shared.postShareTitle, description: model.title, imageUrl: "https://www.77.top/static/ios_logo.jpg", url: model.share_url)
        }else if type == 4 {
            "分享口令已复制成功".toast_success
            let shareUrl = model.share_url
            UIPasteboard.general.string = shareUrl
        }else{
            ClubGlobalTool.shared.shareToWechat(title: ClubGlobalTool.shared.postShareTitle, des: model.title, image: "AppIcon".toImage!, shareUrl: model.share_url, shareType: type)
        }
    }
}


//MARK: data
extension ClubOtherInfoViewController {

    private func loadData(){
        
        Task {
            ToastUtil.show()
            let res = await network.request(.mineInfo(uid: uid),type: ClubMineModel.self)
            if res.isSuccess, let model = res.model {
                let userModel = model.user
                
                self.userModel = userModel
                self.nameLabel.text = userModel?.user_name
                var imgStr = "Mine_info_whiteFemale"
                if let gender = userModel?.gender {
                    if gender == "1"{
                        imgStr = "Mine_info_whiteMan"
                    }else if gender == ""{
                        imgStr = "Mine_info_whiteMedium"
                    }
                }
                subLabel.setImage(imgStr.toImage, for: .normal)
                let uid = userModel?.vir_user_id ?? ""
                let subStr = (userModel?.age ?? "") + " | UID: \(uid)"
                subLabel.setTitle(subStr, for: .normal)
                signLabel.text = userModel?.desc
                iconImageView.loadImage(url: userModel?.face)
            }
        }
        
        loadDynamicList()
    }
    
    private func loadDynamicList(){

        ToastUtil.show()
        if self.type == "2" {
            Task {
                let res = await network.request(.otherForumList(userId: uid, page: pageIndex),type: ClubGameCommunityListModel.self)
                ToastUtil.dismiss()
                tableView.refresh_end()
                self.forumData.removeAll()
                if res.isSuccess, let models = res.models {
                    self.forumData = models
                    self.tableView.reloadData()
                    loadFirstVideo()
                }
            }
        }else{
            Task {
                let res = await network.request(.otherDynamicList(uid: uid, page: pageIndex),type: ClubGameCircleModel.self)
                ToastUtil.dismiss()
                tableView.refresh_end()
                self.dataSource.removeAll()
                if res.isSuccess, let models = res.models {
                    if models.count > 0 {
                        for md in models {
                            var mod = md
                            var itemSize: CGSize = .zero
                            if md.video.count > 0 {
                                itemSize = CGSize (width: CGFloat.screenWidth - 104.0.Scale, height: 152.0.Scale)
                            }else if md.img.count == 1 {
                                itemSize = CGSize (width: CGFloat.screenWidth - 104.0.Scale, height: 152.0.Scale)
                            }else if md.img.count == 2 {
                                itemSize = CGSize (width: (CGFloat.screenWidth - 106.0.Scale)/2.0, height: 83.0.Scale)
                            }else if md.img.count > 2 {
                                let w = (CGFloat.screenWidth - 110.0.Scale)/3
                                itemSize = CGSize (width: w, height: w)
                            }else{
                                itemSize = .zero
                            }
                            mod.itemSize = itemSize
                            self.dataSource.append(mod)
                        }
                        if models.count > 20 {
                            self.tableView.bounces = true
                        }
                    }
                    self.tableView.reloadData()
                    loadFirstVideo()
                }
            }
        }
    }
    
    @objc func loadNewData(){
        pageIndex = 1
        loadDynamicList()
    }
    
    @objc func loadMoreData(){
        pageIndex += 1
        if type == "1" {
            Task {
                let models = await network.request(.otherDynamicList(uid: uid, page: pageIndex),type: ClubGameCircleModel.self)
                tableView.refresh_end()
                if models.isSuccess, let models = models.models {
                    if models.count > 0 {
                        for md in models {
                            var mod = md
                            var itemSize: CGSize = .zero
                            if md.video.count > 0 {
                                itemSize = CGSize (width: CGFloat.screenWidth - 104.0.Scale, height: 152.0.Scale)
                            }else if md.img.count == 1 {
                                itemSize = CGSize (width: CGFloat.screenWidth - 104.0.Scale, height: 152.0.Scale)
                            }else if md.img.count == 2 {
                                itemSize = CGSize (width: (CGFloat.screenWidth - 106.0.Scale)/2.0, height: 83.0.Scale)
                            }else if md.img.count > 2 {
                                let w = (CGFloat.screenWidth - 110.0.Scale)/3
                                itemSize = CGSize (width: w, height: w)
                            }else{
                                itemSize = .zero
                            }
                            mod.itemSize = itemSize
                            self.dataSource.append(mod)
                        }
                    }else{
                        tableView.refresh_end(true)
                    }
                    self.tableView.reloadData()
                }
            }
        }else{
            
            Task {
                let res = await self.network.request(.otherForumList(userId: uid, page: pageIndex),type: ClubGameCommunityListModel.self)
                tableView.refresh_end()
                if res.isSuccess, let models = res.models {
                    if models.count > 0 {
                        self.forumData.append(contentsOf: models)
                    }else{
                        tableView.refresh_end(true)
                    }
                    self.tableView.reloadData()
                }
            }
        }
        
    }
    
    private func blackUser() {
        if let uer_id = UserDefaultsManager.getString(forKey: .uid), uer_id.count > 0 {
            Task {
                let models = await network.request(.addBlackList(uid: self.uid),isShowError: false)
                if models.isSuccess {
                    if self.isDetailFrom {
                        if let navigationController = self.navigationController {
                            // 假设你想要跳转到的视图控制器是DetailViewController
                            for viewController in navigationController.viewControllers {
                                if viewController is ClubSquareViewController {
                                    DispatchQueue.main.async {
                                        NotificationCenter.default.post(name: .updateDynamicListData, object: nil)
                                    }
                                    navigationController.popToViewController(viewController, animated: true)
                                    break
                                }
                            }
                        }
                    }else{
                        self.updateBlock?()
                        self.navigationController?.popViewController(animated: true)
                    }
                }
            }
        }else{
            self.navigationController?.pushViewController(ClubLoginViewController(), animated: true)
        }
        
    }
    
    private func loadFirstVideo(){
        
        if currentVC {
            if type == "1" {
                if self.dataSource.count > 0 {
                    let md = self.dataSource[0]
                    if md.video.count > 0 {
                        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                            let indexPath = IndexPath(row: 0, section: 0)
                            self.playingIndexPath = indexPath
                            let cell = self.tableView.cellForRow(at: indexPath) as! ClubDynamicListCell
                            cell.playVideo()  // 播放视频
                        }
                    }
                }
            }else{
                if self.forumData.count > 0 {
                    let md = self.forumData[0]
                    if md.video.count > 0 {
                        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                            let indexPath = IndexPath(row: 0, section: 0)
                            self.playingIndexPath = indexPath
                            let cell = self.tableView.cellForRow(at: indexPath) as! ClubDynamicListCell
                            cell.playVideo()  // 播放视频
                        }
                    }
                }
            }
        }
    }
}
